/*
 * 文 件 名:  ServerStop.java
 * 版    权:  ETOC 信息技术有限公司, Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  Administrator
 * 修改时间:  2015-4-21
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.etoc.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2015-4-21]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class ServerMoniter implements Runnable
{
    
    private static final Logger logger = Logger.getLogger(ServerMoniter.class);
    
    ServerSocket server = null;
    
    Socket socket = null;
    
    InputStream in = null;
    
    BufferedReader br = null;
    
    @Override
    public void run()
    {
        
        try
        {
            server = new ServerSocket(10881);
            logger.info("Server stop listener == > \t port = > " + 10881);
            socket = server.accept();
            logger.info("Server stop action   == > \t port = > " + 10881);
            in = socket.getInputStream();
            //InputStreamReader将字节流转化为字符流
            br = new BufferedReader(new InputStreamReader(in));
            //行读取客户端数据
            String info = br.readLine();
            logger.info(info);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            close(server, socket, in, br);
            
            try
            {
                Thread.sleep(5000l);
            }
            catch (InterruptedException e)
            {
                logger.error(e);
            }
            
            System.exit(1);
        }
    }
    
    private void close(ServerSocket server, Socket socket, InputStream in, BufferedReader br)
    {
        try
        {
            if (null != br)
                br.close();
        }
        catch (IOException e)
        {
            logger.error(e);
        }
        try
        {
            if (null != in)
                in.close();
        }
        catch (IOException e)
        {
            logger.error(e);
        }
        try
        {
            if (null != socket && !socket.isClosed())
                socket.close();
        }
        catch (IOException e)
        {
            logger.error(e);
        }
        try
        {
            if (server != null && !server.isClosed())
                server.close();
        }
        catch (IOException e)
        {
            logger.error(e);
        }
    }
}
